﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Masood.Utilities.EmailTemplates.Items;
using Microsoft.SharePoint;

namespace Masood.Utilities.EmailTemplates.Manager
{
    public class SharePointTemplateManager : AbstractTemplateManager
    {
        private string siteCollectionUrl;
        private string templateListName;

        public SharePointTemplateManager(string siteCollectionUrl, string templateListName)
        {
            this.siteCollectionUrl = siteCollectionUrl;
            this.templateListName = templateListName;
        }

        public override Items.EmailTemplate GetTemplate(string templateId)
        {
            EmailTemplate emailTemplate = new EmailTemplate();            

            SPSecurity.RunWithElevatedPrivileges(() =>
            {
                using (SPSite site = new SPSite(siteCollectionUrl))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList templateList = web.Lists.TryGetList(templateListName);
                        if (templateList == null)
                            throw new ArgumentNullException(string.Format("Template list '{0}' could not be found. Please create it before using.", templateListName));

                        SPQuery spQuery = new SPQuery();
                        //may need to correct or update this CAML to find the right Template
                        spQuery.Query = string.Format("<Where><Eq><FieldRef Name='TemplateId'><Value Type='Text'>{0}</Value></FieldRef></Eq></Where>", templateId);
                        SPListItemCollection templateItems = templateList.GetItems(spQuery);

                        if (templateItems.Count > 1)
                            throw new ArgumentException("Multiple template found for given template id.");

                        SPListItem templateItem = templateItems[0];

                        emailTemplate.Subject = templateItem["Subject"].ToString();
                        emailTemplate.Body = templateItem["Body"].ToString();
                        emailTemplate.TemplateId = templateItem["TemplateId"].ToString();
                    }
                }
            });
             

            return emailTemplate;
        }

        public override void BeforeApplyingTemplateOnSubject(Items.EmailTemplate template, ref Items.Email email, Items.IItem anItem)
        {
            
        }

        public override void AfterApplyingTemplateOnSubject(Items.EmailTemplate template, ref Items.Email email, Items.IItem anItem)
        {
            
        }

        public override void BeforeApplyingTemplateOnBody(Items.EmailTemplate template, ref Items.Email email, Items.IItem anItem)
        {
            
        }

        public override void AfterApplyingTemplateOnBody(Items.EmailTemplate template, ref Items.Email email, Items.IItem anItem)
        {
            
        }
    }
}
